//
//  RadioViewController.m
//  时光
//
//  Created by dllo on 16/3/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "RadioViewController.h"
#import "RadioCollectionViewCell.h"
#import "RadioModel.h"
#import "RadioMuluViewController.h"


@interface RadioViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) NSMutableArray *array1;

@end

@implementation RadioViewController

-(NSMutableArray *)array1{
    if (_array1 == nil) {
        _array1 = [NSMutableArray array];
    }
    return _array1;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self creatCollection];
}

- (void)creatCollection{
   UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH,  HEIGHT - 64) collectionViewLayout:layout];
    self.collection.dataSource = self;
    self.collection.delegate = self;
    [self.view addSubview:self.collection];
    self.collection.backgroundColor = [UIColor whiteColor];
   layout.itemSize = CGSizeMake(190 * W, 170 * H);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 20);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    
    [self.collection registerClass:[RadioCollectionViewCell class] forCellWithReuseIdentifier:@"RadioCollectionViewCell"];
    
    self.collection.mj_header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
   self.collection.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(getOldData)];
    
    [self.collection.mj_header beginRefreshing];
}



- (void)getData{
[MHNetWorkTask getWithURL:@"http://wawa.fm:9090/wawa/api.php/user/gethotplaylist?datatype=jsonp&r=10&page=1&callback=data&_=1459133646101" withParameter:nil
withHttpHeader:@{@"Host" : @"wawa.fm:"} withResponseType:ResponseTypeData withSuccess:^(id result) {
    NSString *str = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];

    NSString *str1 = [str substringFromIndex:7];
    NSString *str2 = [str1 substringToIndex:str1.length - 1];

    
    NSData *data = [[NSData alloc] initWithData:[str2 dataUsingEncoding:NSUTF8StringEncoding]];
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    NSMutableArray *temp = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        RadioModel *model = [[RadioModel alloc] initWithDic:dic];
        [temp addObject:model];
        }
    self.array1 = [NSMutableArray arrayWithArray:temp];
    [self.collection reloadData];
   
    [self.collection.mj_header endRefreshing];
    
    
} withFail:^(NSError *error) {
    NSLog(@"%@", error);
    
}];

}

- (void)getOldData{
    NSInteger num = self.array1.count / 10 + 1;
    NSString *url = [NSString stringWithFormat:@"http://wawa.fm:9090/wawa/api.php/user/gethotplaylist?datatype=jsonp&r=10&page=%ld&callback=data&_=1459133646101", num];
    [MHNetWorkTask getWithURL:url withParameter:nil
               withHttpHeader:@{@"Host" : @"wawa.fm:"} withResponseType:ResponseTypeData withSuccess:^(id result) {
                   NSString *str = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
                   NSArray *json = [str componentsSeparatedByString:@"("];
                   
                   NSArray *json1 = [json.lastObject componentsSeparatedByString:@")"];
                   NSData *data = [[NSData alloc] initWithData:[json1.firstObject dataUsingEncoding:NSUTF8StringEncoding]];
                   NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
                   for (NSDictionary *dic in array) {
                       RadioModel *model = [[RadioModel alloc] initWithDic:dic];
                       [self.array1 addObject:model];
                       
                   }
                   [self.collection reloadData];
                   //    NSLog(@"%ld", self.array1.count);
                   [self.collection.mj_footer endRefreshing];
                   
                   
                   
               } withFail:^(NSError *error) {
                   NSLog(@"%@", error);
                   
               }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array1.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RadioCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RadioCollectionViewCell" forIndexPath:indexPath];
    RadioModel *model = self.array1[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    RadioMuluViewController *mulu = [[RadioMuluViewController alloc] init];
    RadioModel *model = self.array1[indexPath.row];
    mulu.ID = model.ID;
    [self.navigationController pushViewController:mulu animated:YES];
}


- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    SAND
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
